%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%The MATLAB code of the paper "Multi-label Weak-label Learning via Semantic Reconstruction and Label Correlations". 
%nonlinear version
%Contact: daweizhao@ahu.edu.cn
%Data set: 1. Mulan: http://mulan.sourceforge.net/datasets-mlc.html
%          2. Yahoo (LAMDA): http://www.lamda.nju.edu.cn/files/MDDM-expdata.rar
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
clc

addpath('data');
addpath('evaluation');
addpath('function');
starttime = datestr(now,0);

load 'data/emotion.mat';

[optmParameter, modelparameter] =  initialization;% parameter settings for own
%% data processing
data    = [train_data;test_data];
target  = [train_target,test_target]';
target =target';
data      = double (data);
num_data  = size(data,1);
temp_data = data + eps;
temp_data = temp_data./repmat(sqrt(sum(temp_data.^2,2)),1,size(temp_data,2));
if sum(sum(isnan(temp_data)))>0
    temp_data = data+eps;
    temp_data = temp_data./repmat(sqrt(sum(temp_data.^2,2)),1,size(temp_data,2));
end
temp_data = [temp_data,ones(num_data,1)];
target(target==0)=-1;

Result_ours  = zeros(5,modelparameter.cv_num);
 %% cross validation   
    for j = 1:modelparameter.cv_num
        fprintf('Running Fold - %d/%d \n',j,modelparameter.cv_num);
        s = RandStream.create('mrg32k3a','seed',j); %{'dsfmt19937'  'mcg16807' 'mlfg6331_64'   'mrg32k3a'      'mt19937ar' 'shr3cong' 'swb2712'  }
        RandStream.setGlobalStream(s);
       %% the training and test parts are generated by fixed spliting with the given random seed
       randorder = randperm(num_data);
       [cv_train_data,cv_train_target,cv_test_data,cv_test_target ] = generateCVSet( temp_data,target',randorder,j,modelparameter.cv_num );
       
       if modelparameter.misRate > 0
             [ImcompleteTarget, ~, ~, realpercent]= getIncompleteTarget(cv_train_target, modelparameter.misRate,1); 
             fprintf('\n-- Missing rate:%.1f, Real Missing rate %.3f\n',modelparameter.misRate, realpercent); 
             [model] = KWSRLC(cv_train_data,ImcompleteTarget,optmParameter);
       else
             [model] = KWSRLC(cv_train_data,cv_train_target,optmParameter);
       end
        kernel_type='rbf';
        Ktx = kernelmatrix(kernel_type,cv_test_data',cv_train_data',optmParameter.kernel_para); 
        Outputs                = Ktx*model.A;
        fscore                 = model.Kx*model.A;
        [ tau,  currentResult] = TuneThreshold( fscore', cv_train_target');
        Pre_Labels             = Predict(Outputs',tau); 

       %% evaluation of KWSRLC
        Result_ours(:,j) = EvaluationAll(Pre_Labels,Outputs',cv_test_target');
    end

   %% the average results of KWSRLC
    Avg_Result = zeros(5,2);
    Avg_Result(:,1)=mean(Result_ours,2);
    Avg_Result(:,2)=std(Result_ours,1,2);
    fprintf('\nResults of KWSRLC\n');
    PrintResults(Avg_Result);

endtime = datestr(now,0);
rmpath('data');
rmpath('evaluation');
rmpath('function');